package com.zhiyou.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zhiyou.model.FYParent;
import com.zhiyou.model.FyTeacher;
import com.zhiyou.service.TeacherService;
import com.zhiyou.service.parentsService;
import com.zhiyou.utils.JsonResult;

@RestController
@RequestMapping("user/")
public class UserController {

	@Autowired
	TeacherService Tservice;
	@Autowired
	parentsService Pservice;
	
		//查询全部老师
		@GetMapping(value="Tlist")
		public JsonResult Tshow(HttpServletRequest req, HttpServletResponse resp){
			return new JsonResult(200, Tservice.selectAllTeacher(), "查询成功！");
		}
		//查询全部家长
		@GetMapping(value="Plist")
		public JsonResult Pshow(HttpServletRequest req, HttpServletResponse resp){
			return new JsonResult(200, Pservice.selectAllParent(), "查询成功！");
		}
		
		/**
		 * 老师登录
		 */
		@GetMapping("Tlogin/{tacher_phone}/{password}")
		public JsonResult Tlogin(@PathVariable("tacher_phone")String tacher_phone, @PathVariable("password")String password,HttpServletRequest req, HttpServletResponse resp){
			FyTeacher teacher = Tservice.loginTeacher(tacher_phone, password);
			req.getSession().setAttribute("TEACHER", teacher);
			if (teacher==null) {
				return new JsonResult(500, "登陆失败！");
			}
			else{
				
				return new JsonResult(200, teacher, "登陆成功！");
			}
			
		}
		
		/**
		 * 家长登录
		 * 
		 */
		@GetMapping("Plogin/{partent_phone}/{partent_password}")
		public JsonResult Plogin(@PathVariable("partent_phone")String partent_phone, @PathVariable("partent_password")String partent_password,HttpServletRequest req, HttpServletResponse resp){
			FYParent parents = Pservice.loginParent(partent_phone, partent_password);
			req.getSession().setAttribute("PARENT", parents);
			if (parents==null) {
				return new JsonResult(500, "登陆失败！");
			}
			else{
				
				return new JsonResult(200, parents, "登陆成功！");
			}
		}
		/**
		 * 根据id查询老师	
		 */
		@GetMapping("selectByTid/{teacher_id}")
		public JsonResult selectByTid(@PathVariable("teacher_id")Integer teacher_id,HttpServletRequest req, HttpServletResponse resp){
			return new JsonResult(200, Tservice.selectByTeacherid(teacher_id), "查询成功！");
		}
		/**
		 * 根据id查询家长
		 */
		@GetMapping("selectByPid/{parent_id}")
		public JsonResult selectByPid(@PathVariable("parent_id")Integer parent_id,HttpServletRequest req, HttpServletResponse resp){
			return new JsonResult(200, Pservice.selectByParentid(parent_id), "查询成功！");
		}
		/**
		 * 修改登录密码(老师)
		 */
		@PutMapping("updateTpass/")
		public JsonResult updateTpass(FyTeacher Teacher,HttpServletRequest req, HttpServletResponse resp){
			FyTeacher teacher=(FyTeacher) req.getSession().getAttribute("TEACHER");
			Tservice.updateTpass(teacher);
			return new JsonResult(200, "修改成功！");
		}
		/**
		 * 修改手机号(老师)
		 */
		@PutMapping("updateTpho/")
		public JsonResult updateTpho(FyTeacher Teacher,HttpServletRequest req, HttpServletResponse resp){
			Tservice.updateTpass(Teacher);
			return new JsonResult(200, "修改成功！");
		}
		/**
		 * 修改登录密码(家长)
		 */
		@PutMapping("updatePpass/")
		public JsonResult updatePpass(FYParent parent,HttpServletRequest req, HttpServletResponse resp){
			Pservice.updatePpass(parent);
			return new JsonResult(200, "修改成功！");
		}
		/**
		 * 修改手机号(家长)
		 */
		@PutMapping("updatePpho/")
		public JsonResult updatePpho(FYParent parent,HttpServletRequest req, HttpServletResponse resp){
			Pservice.updatePpho(parent);
			return new JsonResult(200, "修改成功！");
		}
		/**
		 * 忘记密码(老师)
		 */
		@PutMapping("Tfoget/{teacher_phone}/{password}")
		public JsonResult Tfoget(@Param("teacher_phone")String teacher_phone,@Param("password")String repassword,HttpServletRequest req, HttpServletResponse resp){
			
			Tservice.Tfoget(teacher_phone, repassword);
			return new JsonResult(200, "修改成功！");
		}
		/**
		 * 修改支付密码(老师)
		 */
		@PutMapping("TupdatePay/")
		public JsonResult TupdatePay(FyTeacher teacher,HttpServletRequest req, HttpServletResponse resp){
			
			Tservice.TupdatePay(teacher);
			return new JsonResult(200, "修改成功！");
		}
		/**
		 * 退出(老师)
		 */
		@GetMapping("Texit")
		public JsonResult Texit(HttpServletRequest req, HttpServletResponse resp){
			req.getSession().setAttribute("TEACHER",null);
			return new JsonResult("退出成功！");
		}
		/**
		 * 退出(家长)
		 */
		
			
		@GetMapping("Pexit")
		public JsonResult Pexit(HttpServletRequest req, HttpServletResponse resp){
			req.getSession().setAttribute("PARENT",null);
			return new JsonResult("退出成功！");
		}
		/**
		 * 头像上传(老师)
		 */
		@PostMapping("Tupload")
		public JsonResult upload(MultipartFile file,@PathVariable("teacher_id")Integer teacher_id,HttpServletRequest req,HttpServletResponse rep){
			Tservice.upload(file,teacher_id);	
			return new JsonResult(200, "上传成功！");
		}
		/**
		 * 手机验证(老师)
		 */
		@RequestMapping("send")
		public JsonResult send(String teacher_phone,HttpServletRequest req,HttpServletResponse rep){
			Tservice.sendCode(teacher_phone);
			System.out.println(teacher_phone);
			return new JsonResult(200, "发送成功！");
		}
		
		/**
		 * 修改密码
		 */
		@RequestMapping("updatem")
		public String regist(String teacher_phone,String code,String repassword,HttpServletRequest req,HttpServletResponse rep){
			
			return Tservice.Tupdate(code, teacher_phone, repassword);
		}
			
}
